package com.sky.service.impl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void CartAdd(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车的是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> shoppingCartList = shoppingMapper.list(shoppingCart);
        //如果存在，将数量加一
        if (shoppingCartList != null && shoppingCartList.size() > 0){
            Long CartId = shoppingCartList.get(0).getId();
            shoppingCartList.get(0).setNumber(shoppingCartList.get(0).getNumber()+1);
            shoppingMapper.update(shoppingCartList.get(0).getNumber(),CartId);
        }else {
            //如果不存在，插入一条数据库
            Long dishId = shoppingCart.getDishId();
            if (dishId!=null){
                Dish dish = dishMapper.GetDishById(dishId);
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setImage(dish.getImage());
            }else {
                SetmealVO setmealVO = setmealMapper.select(shoppingCart.getSetmealId());
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setImage(setmealVO.getImage());
            }
            shoppingMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {

        return shoppingMapper.list(null);
    }

    @Override
    public void ShoppingCartClean() {
        Long userId = BaseContext.getCurrentId();
        shoppingMapper.delete(userId);
    }

    @Override
    public void ShoppingCartSub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long dishId = shoppingCart.getDishId();
        List<ShoppingCart> shoppingCartList = shoppingMapper.list(shoppingCart);
        Long cartId = shoppingCartList.get(0).getId();
        shoppingMapper.update(shoppingCartList.get(0).getNumber()-1,cartId);
    }
}
